<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");
class ElectiveController extends Controller {


    public function _initialize()
    {
        if(!check_login()){
            $this->error('登录状态异常,请重新登录','/Admin/Login/login');
        }
    }
    public function index()
    {
        $page       =   I('get.page');
        $cate_id    =   I("get.cate_id");
        if(empty($page))    $page   =   0;


        $ids    =   D("Common")->getCateIds('pro_cate',$cate_id);
        $keywords   =   I("get.keywords");
        if($ids)    $sql    =   "cate_id in ({$ids})";
        else $sql   =   '';
        if($keywords)   {
            $sql['_string']     =   "u.name like '%{$keywords}%'";
        }

        $pages  =   getPage('elective e',$sql,$page,10,5,"course_course as c ON c.id = e.course_id,course_users as u ON u.id = e.users_id");
        $num    =   $pages['page']*10;

        $lists  =   M('elective e')
            ->join("course_course as c ON c.id = e.course_id","left")
            ->join("course_users as u ON u.id = e.users_id","left")
            ->where($sql)->order('e.add_time desc')->limit($num,10)
            ->field("e.id,e.credit,e.comment,e.add_time,c.name couName,c.type type,c.credit coucredit,c.teacher teacher,u.name userName,u.student_id student_id")
            ->select();


        $this->assign("lists",$lists);
        $this->assign("pages",$pages);
        $this->assign("cate_id",$cate_id);
        $this->assign('keywords',$keywords);
        $this->display();
    }

    public function score()
    {
        $page       =   I('get.page');
        $cate_id    =   I("get.cate_id");
        if(empty($page))    $page   =   0;


        $ids    =   D("Common")->getCateIds('pro_cate',$cate_id);
        $keywords   =   I("get.keywords");
        if($ids)    $sql    =   "cate_id in ({$ids})";
        else $sql   =   '';
        if($keywords)   {
            $sql['_string']     =   "u.name like '%{$keywords}%'";
        }

        $pages  =   getPage('elective e',$sql,$page,10,5,"course_course as c ON c.id = e.course_id,course_users as u ON u.id = e.users_id");
        $num    =   $pages['page']*10;

        $lists  =   M('elective e')
            ->join("course_course as c ON c.id = e.course_id","left")
            ->join("course_users as u ON u.id = e.users_id","left")
            ->where($sql)->order('e.add_time desc')->limit($num,10)
            ->field("e.id,e.credit,e.comment,e.add_time,c.name couName,c.type type,c.credit coucredit,c.teacher teacher,u.name userName,u.student_id student_id")
            ->select();


        $this->assign("lists",$lists);
        $this->assign("pages",$pages);
        $this->assign("cate_id",$cate_id);
        $this->assign('keywords',$keywords);
        $this->display();
    }


    public function addCredit()
    {
        if($_POST){

            $data   =   I("post.");

            if($data['credit']) {
                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            $res    =   M("elective")->where(array("id"=>$data['id']))->save($data);
            if($res     !== false){
                $this->success('操作成功',"/Admin/Elective/score");
            }else $this->error("编辑失败");
        }else{
            $id =   I("get.id");
            if($id){
                $info   =   M("elective")->where(array("id"=>$id))->find();

                $this->assign("info",$info);
            }
            $this->display();
        }
    }


    //产品列表
    public function index1()
    {
        $page   =   I("get.page");
        $cate_id    =   I("get.cate_id");
        if(empty($page))    $page   =   0;
        $ids    =   D("Common")->getCateIds('pro_cate',$cate_id);
        $keywords   =   I("get.keywords");
        if($ids)    $sql    =   "cate_id in ({$ids})";
        else $sql   =   '';
        if($keywords)   {
            $sql['_string']     =   "name like '%{$keywords}%'";
        }
        $data   =   D("Common")->getList('course',$sql,'',$page,10,'id desc');
        foreach($data['lists'] as $k=>$v){
            $data['lists'][$k]['day']   =  task_course_day($v['day']);
            $data['lists'][$k]['time']   =  task_course_time($v['time']);
            $data['lists'][$k]['statu']   =  task_course_status($v['status']);
        }


        $this->assign("lists",$data['lists']);
        $this->assign("pages",$data['pages']);
        $this->assign("cate_id",$cate_id);
        $this->assign('keywords',$keywords);
        $this->display();
    }






}